<template>
	<view class="phoneLoginPage">
		<view class="phoneBody">
			<u-field v-model="phoneNum" label="手机号" placeholder="请输入手机号"></u-field>


<!--      <u-field v-model="password" :password="true" label="密码" placeholder="请输入密码"></u-field>-->
			
			<u-field v-model="code" label="验证码" placeholder="请输入验证码">

				<u-button size="mini" slot="right" type="success" @click="getCode">{{codeText}}</u-button>
			</u-field>
			<u-verification-code ref="uCode" @change="codeChange"></u-verification-code>
		</view>
		<view class="loginBtn">
			<u-button type="success" shape="circle" :ripple="true" @click="phoneLogin1" :disabled="loginState" size="medium">登录注册</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phoneNum:'',
				code:'',
        password:'',
				codeText:'',
        basicInfo: {}
			}
		},
		computed:{
			loginState(){
				return !(this.phoneNum!='' && this.code!='');
        // return !(this.phoneNum!='' && this.password!='');
			}
		},
    onLoad(){
      console.log('触发onLoad')
      let user = uni.getStorageSync('ownerInfo')
      if(user) {
        this.basicInfo = user
      }else{
        this.basicInfo = {}
      }
    },
		methods: {
			//获取验证码
			getCode(){
        let currentCommunity = uni.getStorageSync('current_community');
        console.log(currentCommunity)
        let that = this;
        if (!currentCommunity) {
          this.$refs.uMsg.show({
            title: '请先选择社区',
            type: 'warning',
            callback: function() {
              that.selectCommunity();
            }
          });
          return;
        }
				if(!this.$u.test.mobile(this.phoneNum)){
					this.$u.toast('请正确填写手机号');
					return
				}
				if(this.$refs.uCode.canGetCode) {
					//调取后台接口
          let param = {
            mobile:this.phoneNum,
            communityId: currentCommunity.communityId,
            sendType:1
          }
          this.$u.get('/sys/auth/send/code',param).then(res=>{
            if(res.code==0){
              this.$u.toast('发送验证码成功')
              this.$refs.uCode.start();
            }else{
              this.$u.toast(res.msg);
            }
          })
				}
			},
			codeChange(text) {
				this.codeText = text;
			},
      selectCommunity() {
        let that = this;
        this.$u.get('/community/all').then(res => {
          console.log(res)
          that.communities = res.data;
          that.showCommunity = true;
        })

      },

			//手机登录
			phoneLogin(){
        let currentCommunity = uni.getStorageSync('current_community');
        if (!currentCommunity) {
          uni.showToast({
            title: '请先选择社区',
            //将值设置为 success 或者直接不用写icon这个参数
            icon: 'error',
            //显示持续时间为 2秒
            duration: 2000
          })
          return;
        }
				let param = {};
				param.username = this.phoneNum;
        param.password = this.password;
        param.type = 2;
        param.communityId = currentCommunity.communityId;
				param.code = this.code;
				// console.log('手机号登录信息:',param)
        this.$u.post('/sys/auth/login',param).then(res=>{
          if(res.code==0){
            uni.setStorageSync('miniToken', res.data.access_token)
            //设置存储信息
            this.$u.get('/sys/user/info').then(rs=>{
                if(res.code==0){
                  uni.setStorageSync('ownerInfo', rs.data)
                }
            })
            this.$u.route({
              type: 'switchTab',
              url: 'pages/index/index'
            })
          }else{
            this.$u.toast('网络异常,请稍后再试');
          }
        })
				// this.$u.route({
				// 	type:'switchTab',
				// 	url:'pages/index/index'
				// })
			},
      phoneLogin1(){
        let currentCommunity = uni.getStorageSync('current_community');
        if (!currentCommunity) {
          uni.showToast({
            title: '请先选择社区',
            //将值设置为 success 或者直接不用写icon这个参数
            icon: 'error',
            //显示持续时间为 2秒
            duration: 2000
          })
          return;
        }
        let param = {};
        param.mobile = this.phoneNum;
        // param.password = this.password;
        param.type = 2;
        param.communityId = currentCommunity.communityId;
        param.code = this.code;
        // console.log('手机号登录信息:',param)
        this.$u.post('/sys/auth/mobile',param).then(res=>{
          if(res.code==0){
            uni.setStorageSync('miniToken', res.data.access_token)
            //设置存储信息
            this.$u.get('/sys/user/info').then(rs=>{
              if(res.code==0){
                uni.setStorageSync('ownerInfo', rs.data)
              }
            })
            this.$u.route({
              type: 'switchTab',
              url: 'pages/index/index'
            })
          }else{
            this.$u.toast('网络异常,请稍后再试');
          }
        })
        // this.$u.route({
        // 	type:'switchTab',
        // 	url:'pages/index/index'
        // })
      }
		}
	}
</script>

<style lang="scss">
.phoneLoginPage{
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	margin-top: 150rpx;
	.phoneBody{
		
	}
	.loginBtn{
		margin-top: 50rpx;
	}
}
</style>
